package com.longdaji.decoration.ui.goodscategory.content.list;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.GoodsBean;
import com.longdaji.decoration.ui.detail.GoodsDetailActivity;
import com.longdaji.decoration.utils.ImageUtil;
import com.longdaji.decoration.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class GoodsContentListAdapter extends RecyclerView.Adapter<GoodsContentListAdapter.ViewHolder> {
    private static final String TAG = GoodsContentListAdapter.class.getSimpleName();
    private final Context mContext;
    private List<GoodsBean.Goods> mGoodsList = new ArrayList<>();

    //建立枚举 2个item 类型
    public enum ITEM_TYPE {
        ITEM1,
        ITEM2
    }

    public GoodsContentListAdapter(Context context, List<GoodsBean.Goods> list) {
        if (list != null) {
            this.mGoodsList = list;
        }
        this.mContext = context;
    }

    void setData(List<GoodsBean.Goods> list) {
        if (list != null) {
            this.mGoodsList = list;
        }
        Log.d(TAG, "GoodsContentListAdapter list is" + list);
        Log.d(TAG, "GoodsContentListAdapter mGoodsList is" + mGoodsList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return ITEM_TYPE.ITEM1.ordinal();
        } else {
            return ITEM_TYPE.ITEM2.ordinal();
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM_TYPE.ITEM1.ordinal()) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_category_left, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_category_right, parent, false);
        }
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final GoodsBean.Goods goodItem = mGoodsList.get(position);
        //GlideUtils.loadImage(mContext, goodItem.getImgAddress(), holder.goodsPic, null, R.drawable.no_banner, R.drawable.no_banner);
//        Glide.with(mContext.getApplicationContext())
//                .load(goodItem.getImgAddress())
//                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                .centerCrop()
//               //.placeholder(R.drawable.no_banner)
//                .error(R.drawable.no_banner)
//                .into(holder.goodsPic);
        //ImageUtil.loadIntoUseFitWidth(mContext, goodItem.getImgAddress(), R.drawable.no_banner, holder.goodsPic);
        ImageUtil.loadImage(mContext, goodItem.getImgAddress(), holder.goodsPic);

        holder.goodsName.setText(goodItem.getGoodsName());
        holder.goodsPrice.setText("¥" + StringUtil.formatNumber(goodItem.getGoodsDefaultPrice()));
        holder.goodsItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = GoodsDetailActivity.getStartIntent(mContext);
                intent.putExtra("goods_id", goodItem.getGoodsId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "GoodsContentListAdapter mGoodsList.size is" + mGoodsList.size());
        return mGoodsList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView goodsPic;
        private TextView goodsPrice;
        private TextView goodsName;
        private LinearLayout goodsItem;

        public ViewHolder(View itemView) {
            super(itemView);
            goodsItem = itemView.findViewById(R.id.llyt_goods_item);
            goodsName = itemView.findViewById(R.id.tv_goods_name);
            goodsPrice = itemView.findViewById(R.id.tv_goods_price);
            goodsPic = itemView.findViewById(R.id.iv_goods_pic);
        }
    }

}
